$(function () {
    allStudentTable();
    getGradeList();

})

/*获取年级下拉数据*/
function getGradeList() {
    $.DemoPost("/grade/getGradePageByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":100},
        function (data) {
            if(data.success)
            {
                $.each(data.obj.list,function (i,d) {
                    $("#gradeSelect").append(" <option value='"+d.id+"'>"+d.gradeName+"</option>");
                    $("#gradeselect1").append(" <option value='"+d.id+"'>"+d.gradeName+"</option>");
                    $("#gradeSelect2").append(" <option value='"+d.id+"'>"+d.gradeName+"</option>");
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}

function query(params) {
    var gradeId = $("#gradeSelect").val();
    var classId = $("#classSelect").val();
    var queryVal = $("#queryVal").val();
    var param = {
        queryVar:queryVal,
        gradeId:gradeId,
        classId:classId,
        pageNum : params.offset / params.limit + 1,
        pageSize : params.limit
    };
    return param;
}

/*获取列表数据*/
function allStudentTable() {
    $("#student_list").bootstrapTable({
        url:$.ip+"/student/list?token="+sessionStorage.getItem("token"),
        method:'post',
        queryParams:query,
        sidePagination: "server",
        pagination: true,
        striped:true,
        pageNumber:1,
        pageSize: 10,
        pageList: [10, 25, 50, 100],
        dataType:"json",
        contentType:"application/json",
        responseHandler:function (res) {
            return {
                total:res.obj.total,
                rows :res.obj.list
            }
        },
        columns: [
            {
                title: '序号',
                field: 'id',
                align: 'center',
                valign: 'middle',
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {
                title: '姓名',
                align: 'center',
                field: 'name',
                valign: 'middle',

            }  ,
            {
                title: '性别',
                align: 'center',
                field: 'sex',
                valign: 'middle',

            },
            {
                title: '身高',
                align: 'center',
                field: 'height',
                valign: 'middle',

            },
            {
                title: '体重',
                align: 'center',
                field: 'weight',
                valign: 'middle',

            },
            {
                title: '班级',
                align: 'center',
                field: 'className',
                valign: 'middle',
                formatter:function (className,row) {
                    var gradeName = row.gradeName;
                    return gradeName+""+className;
                }
            },
            {
                title: '年龄',
                align: 'center',
                field: 'age',
                valign: 'middle',

            },
            {
                title: '操作',
                align: 'center',
                field: 'id',
                width:300,
                valign: 'middle',
                formatter:function (id,row) {
                    return '<a onclick="remove(this)" data-id="'+id+'" class="btn btn-danger" href="javascript:;">删除</a>&nbsp<a onclick="info(this)" data-id="'+id+'" class="btn btn-primary " href="javascript:;">查看</a> &nbsp<a onclick="update(this)" data-id="'+id+'" class="btn btn-warning " href="javascript:;">编辑</a>&nbsp<a onclick="setUser(this)" data-id="'+id+'" class="btn btn-primary" href="javascript:;">开设账号</a>';
                }
            },
        ]
    });
}
/*年级下拉菜单的change事件*/
function gradeChang(type) {

    if(type=='1')
    {
        var gradeId = $("#gradeSelect").val();
        if(gradeId=='')
        {
            $("#classSelect").html( "<option value=''>请选择</option>");
            return;
        }
        $.DemoGet("/classInfo/getClassListByGradeId/"+gradeId,function (data) {
            if(data.success)
            {
                $("#classSelect").html( "<option value=''>请选择</option>");
                $.each(data.obj,function (i,d) {
                    $("#classSelect").append(" <option value='"+d.id+"'>"+d.className+"</option>");
                });
            }
            else
            {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("操作失败");
        });
    }
    else if(type=='2')
    {
        var gradeId = $("#gradeselect1").val();
        if(gradeId=='')
        {
            $("#classselect1").html( "<option value=''>请选择</option>");
            return;
        }
        $.DemoGet("/classInfo/getClassListByGradeId/"+gradeId,function (data) {
            if(data.success)
            {
                $("#classselect1").html( "<option value=''>请选择</option>");
                $.each(data.obj,function (i,d) {
                    $("#classselect1").append(" <option value='"+d.id+"'>"+d.className+"</option>");
                });
            }
            else
            {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("操作失败");
        });
    }
    else if(type=='3')
    {
        var gradeId = $("#gradeSelect2").val();
        if(gradeId=='')
        {
            $("#classSelect2").html( "<option value=''>请选择</option>");
            return;
        }
        $.DemoGet("/classInfo/getClassListByGradeId/"+gradeId,function (data) {
            if(data.success)
            {
                $("#classSelect2").html( "<option value=''>请选择</option>");
                $.each(data.obj,function (i,d) {
                    $("#classSelect2").append(" <option value='"+d.id+"'>"+d.className+"</option>");
                });
            }
            else
            {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("操作失败");
        });
    }



}

/*删除*/
function remove(obj){
    var id = $(obj).data("id");
    $.DemoGet("/student/delete/"+id,function (data) {
        if(data.success)
        {
            layer.msg(data.msg);
            $("#student_list").bootstrapTable('refresh');
        }
        else {
            layer.msg(data.msg);
        }
    },function () {
        layer.msg("操作失败");
    });
}
/*添加*/
function add(){
    $("#name").val("");
    $("#classSelect2").val("");
    $("#gradeSelect2").val("");
    $("#height").val("");
    $("#weight").val("");
    $("#studentCode").val("");
    $("#time").val("");
    popdiv("student-info","添加",["50%","80%"],addStudent)
}

function addStudent(index) {

    var name = $("#name").val();
    var sex = $("input[name='sex']:checked").val();
    var classId = $("#classSelect2").val();
    var gradeId = $("#gradeSelect2").val();
    var height = $("#height").val();
    var weight =$("#weight").val();
    var studentCode = $("#studentCode").val();
    var birthday = $("#time").val();

     if(name=='')
    {
        layer.msg("请输入姓名");
        return;
    }
     else if(birthday=='')
     {
         layer.msg("请选择生日");
         return;
     }
    else if(height=='')
    {
        layer.msg("请输入身高");
        return;
    }
    else if(weight=='')
    {
        layer.msg("请输入体重");
        return;
    }
     else if(classId=='')
     {
         layer.msg("请选择班级");
         return;
     }
    $.DemoPost("/student/add",{"name":name,"sex":sex,"classId":classId,"gradeId":gradeId,"height":height,"weight":weight,"studentCode":studentCode,"birthday":birthday},function (data) {
        if(data.success)
        {
            layer.close(index);
            layer.msg(data.msg);
            $("#student_list").bootstrapTable('refresh');
        }
        else {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("添加失败");
    });
}
/*修改*/
function update(obj){
    var id = $(obj).data("id");

    $.DemoGet("/student/getStudentInfo/"+id,function (data) {
        if(data.success)
        {
             $("#studenId").val(data.obj.id);
             $("#name").val(data.obj.name);
             if(data.obj.sex=='0')
             {
                 $("input[name='sex'][value='0']").attr("checked",true);
             }
            else {
                 $("input[name='sex'][value='1']").attr("checked",true);
             }
            $("#gradeSelect2").val(data.obj.gradeId);
             $("#height").val(data.obj.height);
             $("#weight").val(data.obj.weight);
             $("#studentCode").val(data.obj.studentCode);
             $("#time").val(data.obj.birthdayStr);
             var classId = data.obj.classId;
            $.DemoGet("/classInfo/getClassListByGradeId/"+data.obj.gradeId,function (data) {
                if(data.success)
                {
                    $("#classSelect2").html( "<option value=''>请选择</option>");
                    $.each(data.obj,function (i,d) {
                        $("#classSelect2").append(" <option value='"+d.id+"'>"+d.className+"</option>");
                    });
                    $("#classSelect2").val(classId);
                }
                else
                {
                    layer.msg(data.msg);
                }
            },function (e) {
                layer.msg("操作失败");
            });
        }
        else {
            layer.msg(data.msg);
        }
    },function () {
        layer.msg("操作失败");
    })
    popdiv("student-info","修改",["50%","80%"],updateStudent)
}

function updateStudent(index,layero)
{
    var id = $("#studenId").val();
    var name = $("#name").val();
    var sex = $("input[name='sex']:checked").val();
    var classId = $("#classSelect2").val();
    var gradeId = $("#gradeSelect2").val();
    var height = $("#height").val();
    var weight =$("#weight").val();
    var studentCode = $("#studentCode").val();
    var birthday = $("#time").val();
    if(name=='')
    {
        layer.msg("请输入姓名");
        return;
    }
    else if(birthday=='')
    {
        layer.msg("请选择生日");
        return;
    }
    else if(height=='')
    {
        layer.msg("请输入身高");
        return;
    }
    else if(weight=='')
    {
        layer.msg("请输入体重");
        return;
    }
    else if(classId=='')
    {
        layer.msg("请选择班级");
        return;
    }
    $.DemoPost("/student/update",{"id":id,"name":name,"sex":sex,"classId":classId,"gradeId":gradeId,"height":height,"weight":weight,"studentCode":studentCode,"birthday":birthday},function (data) {
        if(data.success)
        {
            layer.close(index);
            $("#student_list").bootstrapTable('refresh');
        }
        layer.msg(data.msg);
    },function (e) {
        layer.msg("修改失败");
    });
}

/*查看*/
function info(obj) {
    var id = $(obj).data("id");
    $.DemoGet("/student/getStudentInfo/"+id,function (data) {
        if(data.success)
        {
            $("#name_lab").text(data.obj.name);
            $("#studentCode_lab").text(data.obj.studentCode);
            $("#sex_lab").text(data.obj.sexVal);
            $("#birthday_lab").text(data.obj.birthdayStr);
            $("#age_lab").text(data.obj.age);
            $("#height_lab").text(data.obj.height+"CM");
            $("#weight_lab").text(data.obj.weight+"KG");
            $("#bmr_lab").text(data.obj.bmrVal);
            $("#bmi_lab").text(data.obj.bmiVal);
            $("#class_lab").text(data.obj.gradeName+""+data.obj.className);
        }
        else {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("操作失败");
    });
    popdiv("student-cha","查看",["50%","80%"],function (index,layero) {
        layer.close(index);
    })
}
/*导出*/
function exprot() {

    tan("导出可能会使用部分时间，是否确认导出？",function () {
        var queryVal = $("#queryVal").val();
        var gradeSelect = $("#gradeSelect").val();
        var classSelect = $("#classSelect").val();
        location.href = $.ip+"/student/export?token="+sessionStorage.getItem("token")+
            "&queryVar="+queryVal+"&gradeId="+gradeSelect+"&classId"+classSelect;
    });

}
/*导入*/
function out(){
    $("#classselect1").val("");
    $("#gradeselect1").val("");
    $("#file").val("");
    popdiv("export","导出",["50%","50%"],ajaxFileUpload)
}
//时间
getTime("time");

$("#btnQuery").click(function () {
    $("#student_list").bootstrapTable('refresh');
});

function ajaxFileUpload(index,k) {
    var classId = $("#classselect1").val();
    if(classId=='')
    {
        layer.msg("请选择班级");
        return ;
    }
    $.ajaxFileUpload(
        {
            url: $.ip+'/student/upload', //用于文件上传的服务器端请求地址
            type: 'post',
            data: {classId: classId }, //此参数非常严谨，写错一个引号都不行
            secureuri: false, //一般设置为false
            fileElementId: 'file', //文件上传空间的id属性  <input type="file" id="file" name="file" />
            dataType: 'json', //返回值类型 一般设置为json
            success: function (data, status)  //服务器成功响应处理函数
            {
                layer.msg("上传成功");
                layer.close(index);
                $("#student_list").bootstrapTable('refresh');
            },
            error: function (data, status, e)//服务器响应失败处理函数
            {
                layer.msg("上传失败");
            }
        }
    )
    return false;
}


function setUser(obj) {
    $("#userName").val("");
    var id = $(obj).data("id");
    $("#mainId").val(id);
    $.DemoGet("/sysUser/getStudentUserInfo/"+id,function (data) {
        if(data.success)
        {
            if(data.obj!=null && data.obj.userName!='')
            {
                $("#userName").val(data.obj.userName);
                $("#userName").attr("readonly","readonly");
            }
        }
        else{
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("获取账户信息失败");
    });
    popdiv("student-user","开设账号",["50%","30%"],addUserInfo)
}

function addUserInfo(index,k) {
    var userName =  $("#userName").val();
    var mainId = $("#mainId").val();
    $.DemoPost("/sysUser/registerStudent",
        {"userName":userName,"main_id":mainId},
        function (data) {
            if(data.success)
            {
                layer.close(index);
                $("#teacher_list").bootstrapTable('refresh');
            }
            layer.msg(data.msg);
        },function (e) {
            layer.msg("添加账号失败");
        });
}